<template>
	<view class="flex-col page">
		<view>
			 <view :style="{height:statusBarHeight+'px'}"></view>
		
			<u-navbar title="绵州商城" :bgColor="setBgColor()">
			 	<view slot="left" class="nav-left">
			 	</view>
			 </u-navbar>
	
			<view class="nav-top">
			    <view class="header">
			        <view class="search-box" @click="$nav(1,'/pages/search/search')">
			            <image class="img" :src="staticResource('home/custom/search.png')" />
			            <view class="input">商品</view>
			            <view class="btn">搜索</view>
			        </view>
			    </view>
			</view> 
		</view>
		
		<view class="content">
			<view class="banner-ad">
			    <uni-swiper-dot :info="banner" :current="current" bottom="-10" field="content" mode="round">
			        <swiper 
			            class="swiper" 
			            autoplay 
			            :duration="300" 
			            :interval="5000" 
			            :indicator-dots="false" 
			            circular
			            @change="onChange">
			            <swiper-item v-for="(item, idx) in banner" :key="idx">
			                <image class="banner-ad-img" :src="item.image_url" mode="aspectFill"/>
			            </swiper-item>
			        </swiper>
			    </uni-swiper-dot>
			</view>
			
			<view class="flex-row justify-around items-center" style="margin-top: 40rpx;">
				<view class="flex-row items-center">
					<image src="/static/recommend_tip_1.png" style="width: 30rpx;height: 30rpx;margin-right: 8rpx;"></image>
					<text style="font-size: 24rpx;color: #555555;">正品保障</text>
				</view>
				<!-- <view class="flex-row items-center">
					<image src="/static/recommend_tip_2.png" style="width: 30rpx;height: 30rpx;margin-right: 8rpx;"></image>
					<text style="font-size: 24rpx;color: #555555;">一件包邮</text>
				</view> -->
				<view class="flex-row items-center">
					<image src="/static/recommend_tip_3.png" style="width: 30rpx;height: 30rpx;margin-right: 8rpx;"></image>
					<text style="font-size: 24rpx;color: #555555;">售后无忧</text>
				</view>
				
			</view>
			<!--分类-->
			<view class="nav">
				<view class="item" v-for="(item,index) in subNavs" :key="index" @click="subClick(item)">
					<view class="img">
						<image class="img" :src="item.img_url" />
					</view>
					<view class="name">{{ item.category_name }}</view>
				</view>
			</view>
			
			<view class="center-dis-radius">
				<view class="flex-row justify-center items-center">
					<view class="left-radius">
						
						<image src="/static/recommend_tips.png" style="width: 25rpx;height: 25rpx;"></image>
						<text style="margin-left: 10rpx;">保价放心购</text>
					</view>
					<text style="color: #a49482;font-size: 25rpx;margin-left: 30rpx;">单单保价 购物无忧</text>
					
				</view>
				<image src="/static/baojia.png" style="width: 30rpx;height: 30rpx;margin-right: 35rpx;"></image>
			</view>
			
			
			
			<view style="margin: 20rpx;">
				<image @click="activeClick" :src="singleImg.img" style="width: 100%;border-radius: 10rpx;height: 165rpx;margin-top: 20rpx;"></image>
			</view>
			
			<!--推荐-->
			<view class="ads">
			    <view class="ad" v-for="ad in ads" :key="ad.id" @click="adsClick(ad)">
			        <view class="name">{{ ad.name }}</view>
			        <!-- <view class="des">{{ ad.identificationName }}</view> -->
			        <view class="img-box">
			            <image class="img" :src="ad.img" mode="aspectFill" />
			        </view>
			    </view>
			</view>
			
			<view class="tip">
			    <image class="img" :src="staticResource('home/custom/recommend-tip.png')" />
			    <view>好物・甄选</view>
			</view>
			
			<view class="flex-col" v-if="list.length > 0">
				<ProductItem :list="list"></ProductItem>
				
				<u-loadmore margin-top="20" margin-bottom="20" :status="status" icon-type="flower"
					:load-text="loadText" />
			</view>
			
			<view v-else class="flex-col items-center" style="margin-top: 80rpx;">
				<image src="/static/data_empty.png"></image>
				<text style="color: #c0c4cc;">暂无数据</text>
				<!-- <u-empty margin-top='120' mode="history" text="暂无数据"></u-empty> -->
			</view>
			
		</view>
		
		<Tabbar :current-page='1'></Tabbar>
	</view>
</template>

<script>
	import ProductItem from '@/components/product-item.vue';
	export default {
		data() {
			return {
				menuHeight:0,
				current:0,
				banner:[],
				ads:[],
				list:[],
				subNavs:[],
				page:1,
				status: 'loadmore',
				loadText: {
					loadmore: '下拉加载更多...',
					loading: '加载中...',
					nomore: '没有更多数据了...'
				},
				singleImg:{}
			}
		},
		components:{
			ProductItem
		},
		onLoad() {
			
			// this.getProductGroup();
			// this.getProductList();
			
			this.list = [];
			this.page = 1;
			this.getProductList();
			this.getProductBanner();
			this.getCateGory();
			this.getPrductAds();
		},
		onShow() {
			
			
		},
		onPullDownRefresh() {
			this.list = [];
			this.page = 1;
			this.getProductList();
			this.getProductBanner();
			this.getCateGory();
			this.getPrductAds();
		},
		onReachBottom() {
			this.getProductList();
		},
		computed:{
			statusBarHeight(){
				return this.$store.state.statusBarHeight + 44;
			}
		},
		methods: {
			activeClick(){
				uni.navigateTo({
					url: '/pages/active/index?id='+this.singleImg.id+'&name='+this.singleImg.name
				})
			},
			subClick(item){
				if(item.category_name == '果蔬生鲜'){
					uni.navigateTo({
						url:'/pages/recommend/category_list?title='+item.category_name+'&index='+item.category_id
					})
				}else{
					uni.navigateTo({
						url:'/pages/productCate/productCate?pid=' + item.category_id + '&title=' + item.category_name
					})
				}
			},
			adsClick(item){
				uni.navigateTo({
					url: '/pages/active/index?id='+item.id+'&name='+item.name
				})
			},
			getPrductAds(){
				this.$request({
					url:'?c=Active&a=shopActiveList',
					type:'post',
				}).then(res =>{
					let data = res.data;
					let firstElement = data[0];
					let lastTwoElements = data.slice(-2);
					this.singleImg = firstElement;
					this.ads = lastTwoElements;
				})
			},
			
			
			getCateGory(){
				this.$request({
					url:'?c=Goods&a=category',
					type:'post'
				}).then(res =>{
					this.subNavs = res.data;
				})
			},
			
			
			getProductList(){
				this.$request({
					url:'?c=Goods&a=goodsList',
					type:'post',
					data:{
						page:this.page,
						limit:10
					}
				}).then(res =>{
					uni.stopPullDownRefresh();
					this.status = 'loadmore';
					if (res.data && res.data.length > 0) {
						this.list = [...this.list, ...res.data];
						this.page += 1;
						if (res.data.length < 10) {
							this.status = 'nomore';
						}
					} else {
						this.status = 'nomore';
					}
				})
			},
			
			///等于1是首页的商品分组 等2是商城的首页分组
			// getProductGroup(){
			// 	this.$request({
			// 		url:'product/product/group',
			// 		type:'get',
			// 		data:{
			// 			productGroupType: 2 
			// 		}
			// 	}).then(res =>{
			// 		this.ads = res.data.records;
			// 	});
			// },
			getProductBanner(){
				this.$request({
					url:'?c=Index&a=carousel',
					type:'post',
					data:{
						identification:'mzsc'
					}
				}).then(res =>{
					this.banner = res.data;
				});
			},
			onChange(e){
				this.current = e.detail.current;
			},
			staticResource(path){
				return "https://ysmz.scmzhc.com/static/mini/images/"+path;
			},
			setBgColor(){
				return "#F3D0CC"
			}
		}
	}
</script>

<style lang="scss" scoped>
.page {
	width: 100%;
	overflow-y: auto;
	overflow-x: hidden;
	height: 100%;
	padding-bottom: 100rpx;
}
.nav-top {
    // background: linear-gradient(90deg, #F5ECEC, #FEB0AB);
	background-color: #F3D0CC;
    border-radius: 0 0 20rpx 20rpx;
    color: #ffffff;
}

.header {
    display: flex;
    align-items: center;
    padding: 20rpx 32rpx 166rpx 18rpx;
    .search-box {
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #ffffff;
        height: 70rpx;
        border-radius: 35rpx;
        flex: 1;
        .img {
            width: 32rpx;
            height: 32rpx;
            margin-left: 24rpx;
            margin-right: 15rpx;
        }
        .input {
            flex: 1;
            text-align: left;
            color: #999999;
            font-size: 28rpx;
        }
        .btn {
            width: 109rpx;
            color: #32353D;
            font-size: 28rpx;
            text-align: center;
            border-left: 3rpx solid #E05151;
            line-height: 26rpx;
        }
    }
}
.content{
	position: relative;
	top: -134rpx;
	margin-bottom: -134rpx;
	.nav {
	    margin: 20rpx;
	    display: flex;
	    flex-wrap: wrap;
	    gap: 43rpx 0;
		background-color: #fff;
		border-radius: 12rpx;
	    padding-top: 20rpx;
		padding-bottom: 20rpx;
	    .item {
	        width: 20%;
	        display: flex;
	        flex-direction: column;
	        align-items: center;
	        justify-content: center;
	        gap: 15rpx;
	        .img {
	            width: 70rpx;
	            height: 70rpx;
	        }
	        .name {
	            color: #32353D;
	            font-size: 25rpx;
	        }
	    }
	}
	.ads {
	    // overflow-x: auto;
	    display: flex;
	    align-items: center;
		justify-content: space-between;
		margin-top: 30rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
	    .ad {
	        // width: 45%;
	        padding: 12rpx;
	        border-radius: 10px;
	        background-image: linear-gradient(rgb(255, 255, 255) -50%, rgb(255, 255, 255));
	        .name {
	            font-size: 28rpx;
	            font-weight: 700;
	            margin-right: 10rpx;
	            height: 18px;
				 margin-top: 6rpx;
			    margin-bottom: 30rpx;
	        }
	        .des {
	            margin-top: 6rpx;
	            line-height: 36rpx;
	            overflow: hidden;
	            margin-bottom: 10rpx;
	            color: rgb(218, 79, 51);
	            font-size: 20rpx;
	            height: 36rpx;
	        }
	        .img-box {
	            width: 318rpx;
	            height: 212rpx;
	            border-radius: 20rpx;
	            overflow: hidden;
	            .img {
	                width: 100%;
	                height: 100%;
	            }
	        }
	    }
	}
}
.banner-ad{
    box-sizing: border-box;
    .swiper{
        width: 100%;
        height: 250rpx;
        // padding: 0 25rpx;
        box-sizing: border-box;
    }
    .banner-ad-img{
        margin: 0 25rpx;
        box-sizing: border-box;
        width: calc(100% - 50rpx);
        height: 100%;
        border-radius: 20rpx;
        background-color: gray;
        box-shadow: 0rpx 5rpx 18rpx 2rpx rgba(90,115,241,0.44);
    }
}
.tip {
    margin: 30rpx 23rpx 30rpx;
    font-size: 30rpx;
    font-weight: 500;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 12rpx;
    color: #32353D;
    .img {
        width: 43rpx;
        height: 43rpx;
    }
}
.center-dis-radius{
	// width: 100%;
	margin: 30rpx 20rpx 20rpx 20rpx;
	background-color: #e6e6e6;
	border-radius: 40rpx;
	height: 50rpx;
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
}
.left-radius{
	border-radius: 40rpx;
	height: 50rpx;
	width: 240rpx;
	color: #fff;
	font-size: 25rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #a49482;
	
}
</style>
